package com.linkzd.iot.service.Impl;

import com.linkzd.common.utils.DateUtils;
import com.linkzd.iot.domain.DeviceDataLog;
import com.linkzd.iot.domain.DeviceInfo;
import com.linkzd.iot.domain.SensorsReportDataLog;
import com.linkzd.iot.mapper.DeviceDataLogMapper;
import com.linkzd.iot.mapper.SensorsReportDataLogMapper;
import com.linkzd.iot.model.MeanTemperatureHumidity;
import com.linkzd.iot.service.IDeviceDataLogService;
import com.linkzd.iot.service.ISensorsReportDataLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 数据历史记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-28
 */
@Service
public class SensorsReportDataLogServiceImpl implements ISensorsReportDataLogService
{
    @Autowired
    private SensorsReportDataLogMapper sensorsReportDataLogMapper;

    /**
     * 查询数据历史记录列表
     * 
     * @param sensorsReportDataLog 数据历史记录
     * @return 数据历史记录
     */
    @Override
    public List<SensorsReportDataLog> selectSensorsReportDataLogList(SensorsReportDataLog sensorsReportDataLog)
    {
        return sensorsReportDataLogMapper.selectSensorsReportDataLogList(sensorsReportDataLog);
    }

    /**
     * 查询根据房间号的温湿度均值
     *
     * @param roomNum
     */
    @Override
    public MeanTemperatureHumidity meanTemperatureHumidity(String roomNum) {
        List<Double> barData = new ArrayList<>();//温度均值
        List<String> category = new ArrayList<>();//日期
        List<Double> lineData = new ArrayList<>();//湿度均值
        SensorsReportDataLog sensorsReportDataLog = new SensorsReportDataLog();

        LocalDate today = LocalDate.now();
        for (int i = 7; i >= 1; i--) {
            LocalDate currentDate = today.minusDays(i);
            String dateString = currentDate.toString(); // 格式化日期字符串用于查询
            sensorsReportDataLog.setRoomNum(roomNum);
            sensorsReportDataLog.setCreateTime(dateString);
            List<SensorsReportDataLog> sensorsReportDataLogList = sensorsReportDataLogMapper.selectSensorsReportDataLogList(sensorsReportDataLog);
            category.add(dateString);
            Double  temperature = 0.0;
            Double humidity = 0.0;
            if(sensorsReportDataLogList.size() > 0){

                for (SensorsReportDataLog sensors : sensorsReportDataLogList){
                    String[] datas = sensors.getDeviceData().split(",");
                    if(datas.length == 2){
                        temperature += Double.parseDouble(datas[0].split(":")[1]);
                        humidity += Double.parseDouble(datas[1].split(":")[1]);
                    }
                }
                temperature = temperature/sensorsReportDataLogList.size();
                humidity = humidity/sensorsReportDataLogList.size();
            }

            barData.add(temperature);
            lineData.add(humidity);
        }


        return new MeanTemperatureHumidity(barData,category,lineData);
    }

    /**
     * 查询数据数量
     */
    @Override
    public int selectSensorsReportDataLogNum() {
        return sensorsReportDataLogMapper.selectSensorsReportDataLogNum();
    }


    /**
     * 新增数据历史记录
     * 
     * @param sensorsReportDataLog 数据历史记录
     * @return 结果
     */
    @Override
    public int insertSensorsReportDataLog(SensorsReportDataLog sensorsReportDataLog)
    {
        return sensorsReportDataLogMapper.insertSensorsReportDataLog(sensorsReportDataLog);
    }


    /**
     * 批量删除数据历史记录
     * 
     * @param dataIds 需要删除的数据历史记录主键
     * @return 结果
     */
    @Override
    public int deleteSensorsReportDataLogByDataIds(Long[] dataIds)
    {
        return sensorsReportDataLogMapper.deleteSensorsReportDataLogByDataIds(dataIds);
    }

    /**
     * 删除数据历史记录信息
     * 
     * @param dataId 数据历史记录主键
     * @return 结果
     */
    @Override
    public int deleteSensorsReportDataLogByDataId(Long dataId)
    {
        return sensorsReportDataLogMapper.deleteSensorsReportDataLogByDataId(dataId);
    }


    /**
     * 删除超出三天的历史记录
     *
     * @param date 当前时间
     * @return 结果
     */
    @Override
    public int deleteSensorsReportDataLogByCreateTime(Date date) {
        return sensorsReportDataLogMapper.deleteSensorsReportDataLogByCreateTime(date);
    }

}
